﻿using System;
using Newtonsoft.Json;

namespace DelayedQueue.Models
{
    /// <summary>
    /// 延时任务模型
    /// </summary>
    public class DelayedTask
    {
        /// <summary>
        /// 任务唯一标识
        /// </summary>
        public string TaskId { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 任务内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 预计执行时间（UTC）
        /// </summary>
        public DateTime ExecuteTimeUtc { get; set; }

        /// <summary>
        /// 任务状态
        /// </summary>
        public TaskStatus Status { get; set; } = TaskStatus.Pending;

        /// <summary>
        /// 创建时间（UTC）
        /// </summary>
        public DateTime CreateTimeUtc { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 完成时间（UTC）
        /// </summary>
        public DateTime? CompleteTimeUtc { get; set; }

        /// <summary>
        /// 转换为JSON字符串
        /// </summary>
        public string ToJson()
        {
            return JsonConvert.SerializeObject(this);
        }

        /// <summary>
        /// 从JSON字符串解析
        /// </summary>
        public static DelayedTask FromJson(string json)
        {
            return JsonConvert.DeserializeObject<DelayedTask>(json);
        }
    }

    /// <summary>
    /// 任务状态枚举
    /// </summary>
    public enum TaskStatus
    {
        /// <summary>
        /// 等待中
        /// </summary>
        Pending,

        /// <summary>
        /// 执行中
        /// </summary>
        Executing,

        /// <summary>
        /// 已完成
        /// </summary>
        Completed,

        /// <summary>
        /// 已取消
        /// </summary>
        Cancelled,

        /// <summary>
        /// 执行失败
        /// </summary>
        Failed
    }
}
